package gwt.trading.client.util;


/**
 * @author Iulian Costan (iulian.costan@gmail.com)
 *
 */
public class GwtList {

	public Object[] objects;
	public int index;

	public GwtList() {
		this(10);
	}
	public GwtList(int size) {
		this.objects = new Object[size];
		this.index = 0;
	}
	
	public GwtList(Object[] objects) {
		this.objects = objects;
		this.index = objects.length;
	}

	public void add(Object object) {
		//TODO handle overflow case
		objects[index++] = object;
	}

	public Object getFirst() {
		return index > 0 ? objects[0] : null;
	}

	public Object getLast() {
		return objects.length > 0 ? objects[index - 1] : null;
	}

	public Object get(int idx) {
		return objects.length >= idx ? objects[idx] : null;
	}

	public void forAll(Closure closure) {
		forEach(0, objects.length, closure);
	}
	
	public void forAllExceptEnds(Closure closure) {
		forEach(1, objects.length - 1, closure);
	}

	public void forEach(int start, int end, Closure closure) {
		if (end <= objects.length) {
			for (int i = start; i < end; i++) {
				if (objects[i] != null) {
					closure.execute(objects[i]);
				}
			}
		}
	}

	public int getLength() {
		return objects.length;
	}

}
